import React from "react"

import { connect } from "react-redux";
import * as cityActions from "../redux/actions/city"
import { bindActionCreators } from "redux"
import { withRouter } from "react-router-dom"

import * as util from "../components/util"


class App extends React.Component{

  componentDidMount(){
    let city = this.props.getCityinfo.city;
    let { storage } = util;
    if(!city){   // 首先判断redux中是否有记录城市信息
      city = storage.get("city");
      if(city)  // 其次判断本地是否有记录城市信息
      {
        this.props.setCityinfo.city({
          cityName: city
        });
      }else{
        city = "北京";
        this.props.setCityinfo.city({
          cityName: city
        });
        storage.set("city",city);
      }
    }
  }
  
  render(){
    return(
      <div>
        { this.props.children }
      </div>
    )
  }
}

// 读
function mapStateToprops(state){
	return {
		getCityinfo:state.cityinfo
	}
}

// 写
function mapDispatchToprops(dispatch){
	return {
		setCityinfo: bindActionCreators(cityActions,dispatch)
	}
}

export default withRouter(connect(
	mapStateToprops,
	mapDispatchToprops
)(App))